﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GraphosData;
using System.Xml.Linq;
using GraphosControls;
using System.IO;
using System.Security.AccessControl;

namespace Graphos
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            GraphReader gr = new GraphReader();
            m_graph = gr.OpenFile(@"..\..\..\GraphosData\App_Data\biblepeople2.xml");
            MainGraphViewer.Graph = m_graph;
            MainSearchViewer.AllPossibleValues = m_graph.Nodes.ToList();
            GroupListBox.SizeChanged += new SizeChangedEventHandler(GroupListBox_SizeChanged);
        }

        Graph m_graph;


        void GroupListBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (GroupListBox.Items.Count > 0)
            {
                (sender as ListBox).RaiseEvent(new RoutedEventArgs(ExpanderPanel.MainContentSizeChangedEvent));
            }
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MainGraphViewer.CurrentSelectedRelations = GroupListBox.SelectedItems.Cast<Relation>().ToList();
        }
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            if ((int)e.Key <= 44 && (int)e.Key <= 69 && !SearchExpander.IsOpen)
            {
                SearchExpander.Open();
            }
            else if (e.Key == Key.Escape)
            {
                SearchExpander.Close();
                GroupExpander.Close();
                AnimationExpander.Close();
            }
            else if (e.Key == Key.F1)
            {
                Uri uri = new Uri(@"C:\Users\mitch\Documents\david.png");
                FrameworkElement elem = App.Current.MainWindow;
                ExportToPng(uri, elem);
            }
            else if (e.Key == Key.F12)
            {
                GraphReader gr = new GraphReader();
                m_graph = gr.OpenFile(@"..\..\..\GraphosData\App_Data\Arcitecture.xml");
                MainGraphViewer.Graph = m_graph;
                MainSearchViewer.AllPossibleValues = m_graph.Nodes.ToList();
            }
            else if (e.Key == Key.F11)
            {
                GraphReader gr = new GraphReader();
                m_graph = gr.OpenFile(@"..\..\..\GraphosData\App_Data\biblepeople2.xml");
                MainGraphViewer.Graph = m_graph;
                MainSearchViewer.AllPossibleValues = m_graph.Nodes.ToList();
            }
            e.Handled = false;
        }



        public void ExportToPng(Uri path, FrameworkElement surface)
        {
            if (path == null) return;

            // Save current canvas transform
            Transform transform = surface.LayoutTransform;
            // reset current transform (in case it is scaled or rotated)
            surface.LayoutTransform = null;

            // Get the size of canvas
            Size size = new Size(surface.Width, surface.Height);
            // Measure and arrange the surface
            // VERY IMPORTANT
            surface.Measure(size);
            surface.Arrange(new Rect(size));

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
            renderBitmap.Render(surface);

            PngBitmapEncoder png = new PngBitmapEncoder();

            png.Frames.Add(BitmapFrame.Create(renderBitmap));

            using (Stream stm = File.Create(path.LocalPath))
            {

                png.Save(stm);
            }

            //// Create a file stream for saving image
            //using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
            //{
            //    // Use png encoder for our data
            //    PngBitmapEncoder encoder = new PngBitmapEncoder();
            //    // push the rendered bitmap to it
            //    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            //    // save the data to the stream
            //    encoder.Save(outStream);
            //}

            //// Restore previously saved layout
            //surface.LayoutTransform = transform;
        }
    }
}
